package com.pengheng.mqtt.platform;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.stereotype.Component;

import java.nio.ByteBuffer;

/**
 * 消费监听
 */
@Slf4j
@Component
public class PushCallback implements MqttCallback {

    @Override
    public void connectionLost(Throwable throwable) {

    }

    @Override
    public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
        // subscribe后得到的消息会执行到这里面
//        log.info("接收消息主题 : " + topic);
//        log.info("接收消息Qos : " + mqttMessage.getQos());
        ByteBuffer wrap = ByteBuffer.wrap(mqttMessage.getPayload());
        if ("/gps".equals(topic)) {
            log.info("无人船GPS");
            System.out.println(wrap.getDouble());
            System.out.println(wrap.getDouble());
        }
        if ("/hdt".equals(topic)) {
            log.info("无人船艏向");
            System.out.println(wrap.getFloat());
        }
        if ("/pose".equals(topic)) {
            log.info("无人船姿态");
            System.out.println(wrap.getFloat());
            System.out.println(wrap.getFloat());
            System.out.println(wrap.getFloat());
        }
        if ("/vel".equals(topic)) {
            log.info("无人船速度");
            System.out.println(wrap.getFloat());
        }
        if ("/vtg".equals(topic)) {
            log.info("无人船速度和航向");
            System.out.println(wrap.getFloat());
            System.out.println(wrap.getFloat());
        }
//        log.info("接收消息内容 : " + new String(mqttMessage.getPayload()));
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
        log.info("deliveryComplete---------" + iMqttDeliveryToken.isComplete());
    }
}
